package com.xdb.sqlparser.dialect.oracle.ast.statement;

import com.xdb.sqlparser.ast.statement.SQLExprTableSource;
import com.xdb.sqlparser.dialect.oracle.ast.clause.OracleFlashbackQueryClause;
import com.xdb.sqlparser.dialect.oracle.ast.clause.OraclePartitionExtensionClause;
import com.xdb.sqlparser.dialect.oracle.ast.clause.OracleSampleClause;
import com.xdb.sqlparser.dialect.oracle.visitor.OracleASTVisitor;
import com.xdb.sqlparser.util.SQLUtils;
import com.xdb.sqlparser.visitor.SQLAstVisitor;

public class OracleSelectTableReference extends SQLExprTableSource implements OracleSelectTableSource {

	private boolean only = false;
	protected OracleSelectPivotBase pivot;

	protected OraclePartitionExtensionClause partition;
	protected OracleSampleClause sampleClause;

	protected OracleFlashbackQueryClause flashback;

	public OracleSelectTableReference() {

	}

	public OracleFlashbackQueryClause getFlashback() {
		return flashback;
	}

	public void setFlashback(OracleFlashbackQueryClause flashback) {
		this.flashback = flashback;
	}

	public OraclePartitionExtensionClause getPartition() {
		return partition;
	}

	public void setPartition(OraclePartitionExtensionClause partition) {
		this.partition = partition;
	}

	public boolean isOnly() {
		return this.only;
	}

	public void setOnly(boolean only) {
		this.only = only;
	}

	public OracleSampleClause getSampleClause() {
		return sampleClause;
	}

	public void setSampleClause(OracleSampleClause sampleClause) {
		this.sampleClause = sampleClause;
	}

	public OracleSelectPivotBase getPivot() {
		return pivot;
	}

	public void setPivot(OracleSelectPivotBase pivot) {
		this.pivot = pivot;
	}

	@Override
	protected void accept0(SQLAstVisitor visitor) {
		this.accept0((OracleASTVisitor) visitor);
	}

	protected void accept0(OracleASTVisitor visitor) {
		if (visitor.visit(this)) {
			acceptChild(visitor, this.expr);
			acceptChild(visitor, this.partition);
			acceptChild(visitor, this.sampleClause);
			acceptChild(visitor, this.pivot);
		}
		visitor.endVisit(this);
	}

	public void output(StringBuffer buf) {
		if (this.only) {
			buf.append("ONLY (");
			this.expr.output(buf);
			buf.append(")");
		} else {
			this.expr.output(buf);
		}

		if (this.pivot != null) {
			buf.append(" ");
			this.pivot.output(buf);
		}

		if ((this.alias != null) && (this.alias.length() != 0)) {
			buf.append(this.alias);
		}
	}

	public String toString() {
		return SQLUtils.toOracleString(this);
	}
}
